<?php
namespace Home\Controller;
use Think\Controller;
header('content-type:text/html;charset="UTF-8"');
class ProductController extends Controller {

    public function index(){
    	$id=I('get.id');
    	$db=M('product');
    	$data=$db->where("id={$id}")->find();
    	$data['images']=json_decode($data['images']);	
 		$this->assign('data',$data);
 		//取产品属性组成数组
 		$dc=M('property');
 		$dataProperty=$dc->where("name='{$data['title']}'")->find();
 		$dataProperty['class1']=$dc->where("fid='{$dataProperty['id']}'")->select();
 		foreach ($dataProperty['class1'] as $key => $value) {
 			$value['class2']=$dc->where("fid='{$value['id']}'")->select();
 			$dataProperty['class1'][$key]['class2']=$value['class2'];
 		}
 		$this->assign('dataProperty',$dataProperty);//输出产品的信息
 		$dc=M('shopcart');//统计购物车的数量
        $uid=$_SESSION['uid'];
        if (!empty($uid)) {
            $dataCount=$dc->where("uid={$uid}")->select();
            $count=count($dataCount);
            $this->assign('count',$count);  
        }
 		
 		//输出评论信息
 		$cb=M('comment');
        $ce=M('mallLogin');
 		$dataComment=$cb->where("pid={$id}")->select();
        foreach ($dataComment as $key => $value) {
            $dataLogin=$ce->where("id={$value['uid']}")->find();
            if (empty($dataLogin)) {
               unset($dataComment[$key]);
            }
        }
       
 		$cc=M('order');
 		foreach ($dataComment as $key => $value) {
 			$where = array(
 				'time' =>$value['buytime'] , 
 				'pid'=>$value['pid'],
 				'uid'=>$value['uid']
 				);
 			$commentProperty=$cc->where($where)->find();
 			$commentProperty['property']=json_decode($commentProperty['property'],true);
 			$dataComment[$key]['property']=$commentProperty['property'];
 		}
 		$cd=M('mallLogin');
        foreach ($dataComment as $key => $value) {
	       $data=$cd->where("id={$value['uid']}")->find();
	         $name=$data['email'];
	       if (!empty($data['name'])) {
	          $name=$data['name'];
	       }
	       if (!empty($data['email'])) {
	          $name=$data['email'];
	       }
	       if (!empty($data['phone'])) {
	          $name=$data['phone'];
	       }
	       $dataComment[$key]['name']=$name;
        }
        $cf=M('answercomment');
        foreach ($dataComment as $key => $value) {
    		$data=$cf->where("cid={$value['id']}")->find();
    		$dataComment[$key]['answer']=$data['content'];
        }
 		$this->assign('dataComment',$dataComment);


        //足迹管理
        if (!empty($_SESSION['uid'])) {
            $eb=M('foot');
            $dataFoot['pid']=$id;
            $dataFoot['uid']=$_SESSION['uid'];
            $dataFoot['time']=time();
            $whereFoot = array(
                'uid' =>$dataFoot['uid'] ,
                'pid' =>$dataFoot['pid']  
                );
            $finFoDate=$eb->where($whereFoot)->find();
            if (empty($finFoDate)) {
               $eb->add($dataFoot);
            }else{
                $dataFoot['click']=$finFoDate['click']+1;
                $eb->where("id={$finFoDate['id']}")->save($dataFoot);
            }


            //限定足迹的记录
            $allFoData=$eb->where("uid={$dataFoot['uid']}")->order('time desc')->limit(6,10)->select();
            if (!empty($allFoData)) {
                  foreach ($allFoData as $key => $value) {
                   $eb->where("id={$value['id']}")->delete();
                }
            }
            
        }
        
       

        $this->display();
    }
    
   
}